/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import tools.FileBrowser;
import javafx.scene.input.MouseEvent;
import java.lang.Integer;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.control.TextBox;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.StrokeLineCap;
import javafx.animation.transition.Transition;
import javafx.animation.transition.ScaleTransition;
import control.ControllerPrincipal;
import vinciappli.Templates;
import javafx.scene.Cursor;
import view.ViewPrincipal;

/**
 * @author Sik
 */

public class PlayerScreen extends CustomNode
{
    var urls : String[];
    var avatars : Image[];
    var players : String[];
    var slots : Slot[];

    def bg = 
        Image
        {
            url: "{{Templates.PLAYER_BG_PATH}}"
            backgroundLoading: true
        };

    def background : ImageView =
    ImageView
    {
        image : bg
        x: 0
        y: 0
        fitHeight: Templates.STAGE_HEIGHT
        fitWidth: Templates.STAGE_WIDTH

    };

    var scale : Number;
    var mouseOver :ScaleTransition =
    ScaleTransition 
    {
	duration: 400ms
        fromX: 1 fromY: 1
	
        toX:0.7 toY:0.7
	repeatCount:Transition.INDEFINITE autoReverse: true
    }
    
    function overNavigationAnim(arrow : Polygon) : Void
    {
        mouseOver.node = arrow;
        mouseOver.playFromStart();
    }

    function outNavigationAnim() : Void
    {
        mouseOver.stop();
        mouseOver.time = 400ms;
    }

    def arrowBack : Polygon =
    Polygon
    {
        onMouseEntered: function(e:MouseEvent) : Void
        {
            arrowBack.stroke = Color.BLUE;
            overNavigationAnim(arrowBack);
        }
        onMouseExited : function(e:MouseEvent) : Void
        {
            arrowBack.stroke = Color.WHITE;
            outNavigationAnim();
        }
        onMouseClicked: function(e:MouseEvent) : Void
        {
            ViewPrincipal.previousScreen(this);
        }
        pickOnBounds:true
        translateX: 20
        translateY:Templates.STAGE_HEIGHT - 135
        rotate:180
        scaleX: 0.7
        scaleY: 0.7
	points: [ 0,35, 70,35, 70,0, 110,45, 70,90, 70,55, 0,55 ]
	fill: null
        stroke: Color.WHITE
        strokeWidth: 6
        strokeLineCap: StrokeLineCap.ROUND
        cursor: Cursor.HAND
    }

    def arrowNext : Polygon =
    Polygon
    {
        onMouseEntered: function(e:MouseEvent) : Void
        {
            arrowNext.stroke = Color.BLUE;
            overNavigationAnim(arrowNext);
        }
        onMouseExited : function(e:MouseEvent) : Void
        {
            arrowNext.stroke = Color.WHITE;
            outNavigationAnim();
        }
        onMouseClicked: function(e:MouseEvent) : Void
        {
            for(slot in slots)
            {
                if(not slot.pic.contains("none"))
                {
                    ControllerPrincipal.getINSTANCE().addPlayer(slot.pseudo.text ,slot.pic, true);
                }
            }
            
            ViewPrincipal.nextScreen(this);
            ControllerPrincipal.getINSTANCE().startGame();
            ViewPrincipal.getInstance().buildGameBoard();
        }
        pickOnBounds:true
        translateX: Templates.STAGE_WIDTH - 20 - 110
        translateY:Templates.STAGE_HEIGHT - 135
        scaleX: 0.7
        scaleY: 0.7
	points: [ 0,35, 70,35, 70,0, 110,45, 70,90, 70,55, 0,55 ]
	fill: null
        stroke: Color.WHITE
        strokeWidth: 6
        strokeLineCap: StrokeLineCap.ROUND
        cursor: Cursor.HAND
    }

    function loadAvatars() : Void
    {
        for(s in [0..5])
        {
            players[s] = urls[s];
            var image : Image = bind Image
                {
                    url: urls[s]
                }
            insert image into avatars;
        }
    }

    function loadUrl() : Void
    {
        for(url in FileBrowser.listDirectory(Templates.AVATAR_PATH))
        {
            insert "file:///{{url}}" into urls;
        }
    }

    function switchUrl(pixUrl : String) : String
    {
        var i : Integer = 0;
        for(url in urls)
        {
            if(pixUrl.equals(url))
            {
                if(i == (urls.size()-1))
                {
                    return urls[0];
                }
                else
                {
                    return urls[i+1];
                }
            }
            i++;
        }
        return null;
    }

    function buildSlots() : Slot[]
    {
        var iniX = 125;
        var x = iniX;
        var y = 100;
        def nbSlot = 6-1;
        var slots : Slot[];

        for(i in [0..nbSlot])
        {
            var s : Slot = 
            Slot
            {
                x: x
                y: y
                num: i
                pic : urls[i]
                onMouseClicked: function (e:MouseEvent) : Void
                {
                    s.pic = switchUrl(s.pic);
                }
            }
            insert s into slots;
            
            x += 300;
            if(i == nbSlot/2)
            {
                x = iniX;
                y += 300;
            }
        }
        return slots;
    }



    function GenerateSlot(num : Integer, x : Integer, y : Integer) : Group
    {
        var pseudo : TextBox =
        TextBox
            {
                translateY:200
                text: "Player {{num + 1}}"
                columns: 20
                selectOnFocus: true
            }

        var slot : Group =
        Group
        {
            translateX: x
            translateY: y
            content :
            [
                ImageView
                {
                    onMouseClicked: function (e:MouseEvent) : Void
                    {
                        players[num] = switchUrl(players[num]);
                    }
                    image : bind Image
                    {
                        url : players[num]
                        height:200
                        width: 200
                    }
                }

                Rectangle
                {
                    height: 200
                    width: 200
                    fill: null
                    stroke: Color.WHITE
                    strokeWidth: 4
                }

                pseudo
            ]
        }
        return slot;
    }


    public override function create() : Node
    {

        ini();

        Group
        {
            content:
            [
               background,
               slots, 
               arrowBack, arrowNext
            ];
        };
        
        
    }

    public function ini() : Void
    {
        loadUrl();
        loadAvatars();
        slots = buildSlots();
    }

    public function close() : Void
    {

    }
}
